package edu.gatech.cs4400.spring2014.A8.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import edu.gatech.cs4400.spring2014.A8.control.WaitedOn;

/**
 * Registration screen
 * 
 * @author Stephen
 */
public class RegistrationPanel extends JPanel implements ActionListener, WaitedOn {
	private static final long serialVersionUID = 1L;
	public static enum Feedback{REGISTER, CANCEL};
	private static final String[] accountTypes = {"patient", "doctor", "administrative assis"};
	
	private boolean finished;
	private Feedback feedback;
	
	private JPanel innerPanel;
	private JTextField usernameBox;
	private JTextField passwordBox;
	private JComboBox<String> accountType;
	private JButton registerButton;
	private JButton cancelButton;
	private JLabel messageBox;
	
	/**
	 * Set up the components on the panel
	 */
	public RegistrationPanel() {
		innerPanel = new JPanel();
		usernameBox = new JTextField(20);
		passwordBox = new JTextField(20);
		accountType = new JComboBox<String>(accountTypes);
		registerButton = new JButton("Register");
		cancelButton = new JButton("Cancel");
		messageBox = new JLabel("");
		
		registerButton.addActionListener(this);
		cancelButton.addActionListener(this);
		
		innerPanel.setLayout(new GridLayout(6,1));
		innerPanel.add(usernameBox);
		innerPanel.add(passwordBox);
		innerPanel.add(accountType);
		innerPanel.add(registerButton);
		innerPanel.add(cancelButton);
		innerPanel.add(messageBox);
		this.add(innerPanel);
		
		reset();
	}

	/**
	 * Reset the control and feedback flags 
	 */
	public void reset() {
		finished = false;
		feedback = null;
	}
	
	/**
	 * Set the message to appear on the panel
	 * @param message Message to show
	 */
	public void setMessage(String message) {
		messageBox.setText(message);
	}
	
	/**
	 * Retrieve the username input
	 * @return username
	 */
	public String getUsername() {
		return usernameBox.getText();
	}
	
	/**
	 * Retrieve the password input
	 * @return password
	 */
	public String getPassword() {
		return passwordBox.getText();
	}
	
	/**
	 * Retrieve the account type input
	 * @return account type
	 */
	public String getAccountType() {
		return (String)(accountType.getSelectedItem());
	}
	
	/**
	 * Retrieve the button press 
	 * @return feedback
	 */
	public Feedback getFeedback() {
		return feedback;
	}
	
	/**
	 * Determine if user input was completed and ready for
	 *  interpretation
	 */
	@Override
	public boolean isDone() {
		return finished;
	}

	/**
	 * Respond to button presses
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		finished = true;
		if (arg0.getSource().equals(registerButton)) {
			feedback = Feedback.REGISTER;
		}
		else {
			feedback = Feedback.CANCEL;
		}
		synchronized (this) {
			this.notify();
		}
		
	}
}
